<?php
// +----------------------------------------------------------------------
// | SparkShop 坚持做优秀的商城系统
// +----------------------------------------------------------------------
// | Copyright (c) 2022~2099 http://sparkshop.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai  <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\service;

use app\model\card\Card;
use app\model\card\CardCate;
use app\model\card\CardMod;
use app\model\system\ShippingTemplates;
use app\model\user\UserLabel;
use think\db\exception\DbException;
use think\facade\Db;
use think\response\Json;

class CardModService
{
    /**
     * 模型列表
     * @param array $param
     * @return array
     * @throws DbException
     */
    public function getList(array $param): array
    {
        $limit = $param['limit'];
        $name = $param['name'];
        $cateId = $param['cate_id'];
        $type = $param['tab_type'];

        $where = [];
        if (!empty($name)) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }

        if (!empty($cateId)) {
            $where[] = ['cate_id', '=', $cateId];
        }

        if ($type == 2) {
            $where[] = ['status', '=', 1];
            $where[] = ['is_del', '=', 0];
        } else if ($type == 3) {
            $where[] = ['status', '=', 2];
            $where[] = ['is_del', '=', 0];
        } else if ($type == 4) {
            $where[] = ['is_del', '=', 0];
            $where[] = ['status', '=', 1];
        } else if ($type == 5) {
            $where[] = ['is_del', '=', 1];
        }

        $cardModel = new CardMod();
        $list = $cardModel->where($where)->with('cate')->order('id desc')->paginate($limit);
        return dataReturn(0, 'success', $list);
    }

    /**
     * 添加模型
     * @param array $param
     * @param string $mode
     * @return array
     */
    public function addCardMod(array $param, string $mode): array
    {
        $res = $this->checkParam($param);
        if ($res['code'] != 0) {
            return $res;
        }
        $cardModModel = new CardMod();
        $has = $cardModModel->checkUnique([
            'cate_id' => $param['cate_id'],
            'is_del' => 0,
            'mod' => $param['mod']
        ])['data'];

        if (!empty($has)) {
            return dataReturn(-2, '不能重复设置接口地址');
        }
        $param['create_time'] = now();
        $res = $cardModModel->insertOne($param);
        if($res['code'] < 0){
            return dataReturn(-1, '添加失败');
        }
        return dataReturn(0, '添加成功');
    }

    /**
     * 编辑模型
     * @param array $param
     * @param string $mode
     * @return array
     */
    public function editCardMod(array $param, string $mode): array
    {
        $res = $this->checkParam($param);
        if ($res['code'] != 0) {
            return $res;
        }
        $cardModModel = new CardMod();

        $where[] = ['id', '<>', $param['id']];
        $where[] = ['cate_id', '=', $param['cate_id']];
        $where[] = ['mod', '=', $param['mod']];
        $where[] = ['is_del', '=', 0];
        $has = $cardModModel->checkUnique($where)['data'];

        if (!empty($has)) {
            return dataReturn(-2, '模型已经存在');
        }

        return $cardModModel->updateById($param, $param['id']);
    }

   

    /**
     * 构建基础信息
     * @return array
     */
    public function getBaseParam(): array
    {

        $cardCateModel = new CardCate();
        $cate = $cardCateModel->getAllList([
            'status' => 1
        ], 'id,pid,name', 'id asc')['data']->toArray();


        $labelModel = new UserLabel();
        $labelList = $labelModel->getAllList([], 'id value,name', 'id asc')['data'];

        $shippingTemplatesModel = new ShippingTemplates();
        $list = $shippingTemplatesModel->getAllList([
            'is_del' => 1
        ], 'id,name', 'id asc')['data'];

        return [
            'cate' => makeTree($cate),
            'tpl' => $list
        ];
    }

    /**
     * 删除模型
     * @param int $id
     * @return array
     */
    public function delGoods(int $id): array
    {
        $goodsModel = new Goods();
        $res = $goodsModel->updateByIds([
            'is_show' => 2,
            'is_del' => 1
        ], $id);

        $res['msg'] = '操作成功';
        return $res;
    }

    /**
     * 检测资源的合法性
     * @param array $param
     * @return array
     */
    private function checkParam(array $param): array
    {
        if (empty($param['cate_id'])) {
            return dataReturn(-1, "请选择模型分类", 1);
        }

        if (empty($param['name'])) {
            return dataReturn(-2, "请输入模型地址", 1);
        }

        

        return dataReturn(0);
    }

   
}